// 引入接口文件
import { getPromoteList, queryAllAddress, houseListGet, exportFile } from './promoteTheCustomerServ'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
import { download, makeAddress } from 'utils/util'

// 引入路由对象
import { hashHistory } from 'dva/router'

// 初始默认状态
const defultState = {
  listData: [],
  loading: false,
  exportLoading: false,
  // areaList: [],
  formParams: {
    startTime: '',
    endTime: '',
    // recommendStatus: '',
    // clientStatus: '',
    // city: '',
    // province: '',
    productName: '',
    // area: '',
    dateData: []
  },
  detailData: {
    pageNo: '1',
    total: '0',
    pageSize: '10',
  },
  productIds: [],
}

// 声明module
const tmpModule = {
  namespace: 'promoteTheCustomerModel',
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({pathname, query}) => {
        if ('' + tmpModule.pathname === '' + pathname) {
          dispatch({type: 'getPromoteList', payload: {pageNo: 1, pageSize: 10}})
          dispatch({type: 'getAreaList', payload: {}})
        }
      })
    },
  },
  
  // 方法列表
  effects: {
    //获取级联列表
    * getAreaList ({payload}, {put, call, select}) {
      try {
        const {data, success} = yield call(queryAllAddress)
        if (success) {
          let areaList = []
          let arr = [data[0]]
          let attr = [data[data.length - 1]]
          data[0] = {
            label: '北京',
            value: '110100',
            children: arr
          }
          data[data.length - 1] = {
            label: '澳门',
            value: '820000',
            children: attr
          }
          //去掉第三级，
          areaList = makeAddress(data)
          yield put({type: 'updateStore', payload: {areaList: areaList}})
        }
      } catch (e) {
      
      }
    },
    // 获取列表
    * getPromoteList ({payload}, {put, call, select}) {
      // const { pageNo, pageSize } = payload;
      const {detailData} = yield select(state => state[tmpModule.namespace])
      // yield put({ type: 'updateStore', payload: { pageConfig: { ...pageConfig, ...payload } } });
      if (detailData.pageSize !== payload.pageSize) {payload.pageNo = 1}//改变每页条数
      try {
        yield put({type: 'updateStore', payload: {loading: true, btnLoading: true,}})
        const {rows, success, pageNo, total, pageSize} = yield call(getPromoteList, parse(payload))
        yield put({type: 'updateStore', payload: {loading: false, btnLoading: false,}})
        if (success) {
          yield put({type: 'updateStore', payload: {detailData: {pageNo, total, pageSize: pageSize}, listData: rows}})
        }
      } catch (e) {
      
      }
    },
    //获取项目列表
    * houseListGet ({payload}, {put, call, select}) {
      try {
        const {success, rows} = yield call(houseListGet, {...payload, pageSize: 10000})
        if (success) {
          let productIds = []
          for (let item of rows) {
            productIds.push(item.productId)
          }
          yield put({type: 'updateStore', payload: {productIds: productIds}})
        }
      } catch (e) {
        console.log(e)
      }
    },
    
    //下载模板
    * exportFile ({payload}, {put, call, select}) {
      // let {queryList,productIds,pageConfig} = yield select(state => state[tmpModule.namespace])
      // //组装接口入参
      // let {pageNo=1,pageSize=10}=pageConfig
      // let params = {
      //   productIds,pageNo,pageSize
      // };
      // queryList.map(item => {
      //   if(item.key=='date'){
      //     if(isEmpty(item.value)){
      //       params.startTime=''
      //       params.endTime=''
      //     }else{
      //       params.startTime=item.value[0]
      //       params.endTime=item.value[1]
      //     }
      //   }
      //   else if(item.key=='district'){
      //   }
      //   else{
      //     params[item.key] = item.value;
      //   }
      //
      // });
      // let {extParams = {}, success, msg} = yield call(exportFile, {...params})
      // if (!success) {
      //   message.error(msg)
      //   return false
      // }
      // yield put({ type: 'updateStore', payload: {}});
      // download(extParams.url)
    },
    
  },
  
  // 存入状态机
  reducers: {
    updateStore (preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
